package com.ebao.tec.controller;

import com.ebao.tec.entity.CompanyAndXjd;
import com.ebao.tec.entity.MyResult;
import com.ebao.tec.entity.Xjd;
import com.ebao.tec.service.IndexService;
import com.ebao.tec.vo.ParentCompanyVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.ebao.tec.constant.Code.*;

/**
 * @DESCRIPTION:
 * @Author: WangLt & TianRen Li
 * @Date: 2020/11/27
 */
@RestController
@RequestMapping("/index")
public class IndexControllerImpl {

    @Autowired
    private IndexService indexService;

    /**
     * 公司树形数据获取
     * @return
     */
    @GetMapping("/companys")
    public MyResult listCompany() {
        try{
            List<ParentCompanyVO> list = indexService.listCompany();
            return MyResult.ok().put(DATA, list);
        }catch (Exception e){
            e.printStackTrace();
            return MyResult.error(CODE_ERROR, MSG_CATCH_EXCEPTION);
        }
    }


    /**
     * 折线图一 检修次数的数据获取（暂时按二级公司排列）
     * 折线图二 检修次数的数据获取（按 检修单 检修系统排列）
     * @param companyId
     * @return
     */
    @GetMapping("/root/{companyId}")
    public MyResult RankByCompanyAndJxSys(@PathVariable("companyId") Integer companyId) {
        if(companyId == null || companyId <= 0) {
            return MyResult.error(CODE_ERROR, MSG_PARAMETER_INVALID);
        }
        try {
            CompanyAndXjd companyAndXjd = new CompanyAndXjd(indexService.RankByCompany(companyId), indexService.RankByJxSys(companyId));
            return MyResult.ok().put(DATA, companyAndXjd);
        } catch (Exception e) {
            e.printStackTrace();
            return MyResult.error(CODE_ERROR, MSG_CATCH_EXCEPTION);
        }
    }

    /**
     * 根据公司id获取检修单列表
     * @param companyId
     * @return
     */
    @GetMapping("/xjds/{companyId}")
    public MyResult listXjdByCompanyId(@PathVariable("companyId") Integer companyId) {
        if(companyId==null || companyId <=0){
            return MyResult.error(CODE_ERROR, MSG_PARAMETER_INVALID);
        }
        try{
            List<Xjd> list = indexService.listXjdByCompanyId(companyId);
            return MyResult.ok().put(DATA, list);
        }catch (Exception e){
            e.printStackTrace();
            return MyResult.error(CODE_ERROR, MSG_CATCH_EXCEPTION);
        }

    }
}
